package hamaster.jpbc.pbs;

import hamaster.jpbc.pbc.PBC;
import hamaster.jpbc.pbc.PBCDriver;
import hamaster.jpbc.pbc.PrivateKey;

import java.util.Arrays;
import java.util.Properties;

public class Signature extends PrivateKey {
	public final static String SERVER_SIGNATURE_KEY = "server.sign";
	public final static String SERVER_PARAMETER_KEY = "server.param";
	public final static String SRID_POSTFIX = "srid";
	public final static String SHID_POSTFIX = "shid";
	public final static String HASH_POSTFIX = "hash";
	public final static String HASH_LENGTH_POSTFIX = "length";

	protected byte[] hash;
	protected int hashLength;

	public Signature() {
	}

	public  Signature(Properties properties, String prefix) {
		setFromProperties(properties, prefix);
	}

	public byte[] getHash() {
		return hash;
	}

	public void setHash(byte[] hash) {
		this.hash = hash;
	}

	public void setHashLength(int hashLength) {
		this.hashLength = hashLength;
	}

	public int getHashLength() {
		return hashLength;
	}

	@Override
	public String toString() {
		return "[Signature]\nhash=" + Arrays.toString(hash) + "\nhashLength=" + hashLength
				+ "\nrID=" + rID + "\nhID=" + hID + "\n";
	}

	@Override
	public boolean setFromProperties(Properties properties, String prefix) {
		String srk = SRID_POSTFIX;
		String shk = SHID_POSTFIX;
		String shashk = HASH_POSTFIX;
		String slk = HASH_LENGTH_POSTFIX;
		if (prefix != null && prefix.length() > 0) {
			srk = prefix + "." + SRID_POSTFIX;
			shk = prefix + "." + SHID_POSTFIX;
			shashk = prefix + "." + HASH_POSTFIX;
			slk = prefix + "." + HASH_LENGTH_POSTFIX;
		}
		String rs = properties.getProperty(srk);
		String hs = properties.getProperty(shk);
		String hashs = properties.getProperty(shashk);
		String ls = properties.getProperty(slk);
		if (rs == null || hs == null || hashs == null || ls == null)
			return false;
		PBC pbc = PBC.instance();
		rID = pbc.getPairing().getZr().newElement();
		hID = pbc.getPairing().getG2().newElement();
		hash = PBCDriver.unhex(hashs);
		rID.setFromBytes(PBCDriver.unhex(rs));
		hID.setFromBytes(PBCDriver.unhex(hs));
		try {
			hashLength = Integer.parseInt(ls);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	@Override
	public Properties toProperties(String prefix) {
		String srk = SRID_POSTFIX;
		String shk = SHID_POSTFIX;
		String shashk = HASH_POSTFIX;
		String slk = HASH_LENGTH_POSTFIX;
		if (prefix != null && prefix.length() > 0) {
			srk = prefix + "." + SRID_POSTFIX;
			shk = prefix + "." + SHID_POSTFIX;
			shashk = prefix + "." + HASH_POSTFIX;
			slk = prefix + "." + HASH_LENGTH_POSTFIX;
		}
		Properties properties = new Properties();
		properties.setProperty(srk, PBCDriver.hex(rID.toBytes()));
		properties.setProperty(shk, PBCDriver.hex(hID.toBytes()));
		properties.setProperty(shashk, PBCDriver.hex(hash));
		properties.setProperty(slk, hashLength + "");
		return properties;
	}
}
